﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Deployment.Application;

namespace NicoView.SingleProcess
{
    public static class Parameter
    {
        public static ICollection<string> GetParameters()
        {
            
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                if (ApplicationDeployment.CurrentDeployment == null || ApplicationDeployment.CurrentDeployment.ActivationUri==null) return new string[0];
                var url = ApplicationDeployment.CurrentDeployment.ActivationUri;
                
                var query = url.Query;
                if (String.IsNullOrEmpty(query))
                {
                    return new string[0];
                }

                var nameValuePairs = query.Split('&');
                List<string> args = new List<string>();

                foreach (var argPair in nameValuePairs)
                {
                    string[] arg = argPair.Split('=');
                    if (arg.Length != 2)
                    {
                        continue;
                    }

                    args.Add(arg[1]);

                }

                return args;

            }
            else
            {
                var args = new List<string>();
                var baseArgs=Environment.GetCommandLineArgs();
                for(int index = 1;index < baseArgs.Count();index++)
                {
                    args.Add(baseArgs.ElementAt(index));
                }

                return args;
            }
        }
    }
}
